package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.NursingProject;
import com.itheima.domain.PageResult;
import com.itheima.domain.Result;
import com.itheima.domain.query.NursingProjectQueryParam;
import com.itheima.domain.vo.NursingProjectNameIdVo;
import com.itheima.service.NursingProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/nursingProject")
@RequiredArgsConstructor
public class NursingProjectController {
    private final NursingProjectService nursingProjectService;

    @GetMapping("/all")
    public Result all(){
        List<NursingProjectNameIdVo> list = nursingProjectService.getAllNameId();
        return Result.success(list);
    }

    @PostMapping("/page")
    public Result page(@RequestBody NursingProjectQueryParam nursingProjectQueryParam){
        //获取并封装分页数据
        Page<NursingProject> page = new Page<>(nursingProjectQueryParam.getCurrentPage(),nursingProjectQueryParam.getPageSize());

        // 构建条件查询
        String name = nursingProjectQueryParam.getName();
        Integer orderNo = nursingProjectQueryParam.getOrderNo();
        String unit = nursingProjectQueryParam.getUnit();
        Double price = nursingProjectQueryParam.getPrice();
        String nursingRequirement = nursingProjectQueryParam.getNursingRequirement();

        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),NursingProject::getName,name)
                .eq(orderNo != null,NursingProject::getOrderNo,orderNo)
                .like(StringUtils.hasText(unit),NursingProject::getUnit,unit)
                .ge(price != null,NursingProject::getPrice,price)
                .like(StringUtils.hasText(nursingRequirement),NursingProject::getNursingRequirement,nursingRequirement)
                .orderByAsc(NursingProject::getOrderNo);

        page = nursingProjectService.page(page, queryWrapper);

        return new Result(200,new PageResult(1,page.getRecords(),(int) page.getTotal()),"查询成功");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        NursingProject np = nursingProjectService.getById(id);
        Result result = new Result(0,null,"ID查询失败,无该护理项目");
        if (np != null) {
            result.setCode(200);
            result.setData(np);
            result.setMsg("查询成功");
        }
        return result;
    }

    @PutMapping("/update")
    public Result update(@RequestBody NursingProject nursingProject){
        nursingProject.setUpdateTime(LocalDateTime.now());
        boolean success = nursingProjectService.updateById(nursingProject);
        Result result = new Result(0,null,"修改失败");
        if (success) {
            result.setCode(200);
            result.setMsg("修改成功");
        }
        return result;
    }

    @PostMapping("/add")
    public Result add(@RequestBody NursingProject nursingProject){
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        boolean success = nursingProjectService.save(nursingProject);
        Result result = new Result(0,null,"添加失败");
        if (success) {
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }

    @DeleteMapping("/delete/{ids}")
    public Result deleteByIds(@PathVariable("ids")List<Integer> ids){
        boolean success = nursingProjectService.removeByIds(ids);
        Result result = new Result(0,null,"删除失败");
        if (success) {
            result.setCode(200);
            result.setMsg("删除成功");
        }
        return result;
    }

}


